package com.example.myfoodrecipe.fragments.recipe.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.myfoodrecipe.data.model.ExtendedIngredient
import com.example.myfoodrecipe.databinding.IngredientItemBinding

class IngredientAdapter(ingredients: List<ExtendedIngredient>): RecyclerView.Adapter<IngredientAdapter.ItemViewHolder>() {
    private var ingredientList: List<ExtendedIngredient> = ingredients
    class ItemViewHolder(val binding: IngredientItemBinding): RecyclerView.ViewHolder(binding.root){
        fun bind(ingredient: ExtendedIngredient){
            //dataBinding传递数据
            binding.ingredient = ingredient
            binding.executePendingBindings()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val adapterLayout = LayoutInflater.from(parent.context)
        return ItemViewHolder(IngredientItemBinding.inflate(adapterLayout))
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val ingredient = ingredientList[position]
        holder.bind(ingredient)
    }

    override fun getItemCount(): Int {
        return ingredientList.size
    }
}